@extends("layout.admin")

@section("title")
    添加考试批次成员
@endsection

@section("content")
    <style>
        .layui-laypage-default{
            float: right;
        }
    </style>
    <div class="layui-card-body">
        <div><h3>考场名称：{{$basic}}&nbsp;&nbsp;考试批次：{{$batch}}</h3></div>
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li><a href="{{route('admin.basic.membercreate',['batch_id'=>request('batch_id'),'basic_id'=>request('basic_id')])}}">用户名开通</a> </li>
                <li class="layui-this">条件开通</li>
            </ul>
            <div class="layui-tab-content" style="height: 100px;">
                <form class="layui-form layui-col-space5 layui-input-inline" id="search-form" action="{{route('admin.basic.membercreate.attach')}}">
                    <div class="demoTable">
                        <div class="layui-inline">
                            <input type="hidden" name="batch_id" value="{{request('batch_id')}}">
                            <input type="hidden" name="basic_id" value="{{request('basic_id')}}">
                            <input type="hidden" name="type" value="2">
                            <input type="hidden" name="org_code" id="org_code" value="">
                            <div id="org_id"  style="width: 240px"></div>
                        </div>
                        <div class="layui-inline">
                            <select name="post_id">
                                <option value="">请选择岗位</option>
                                @foreach($post as $vo)
                                    <option value="{{$vo['id']}}" @if($vo['id'] == request('post_id'))selected="selected"@endif>{{$vo['name']}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-inline">
                            <select name="usergroup_id">
                                <option value="">请选择用户组</option>
                                @foreach($user_group as $vo)
                                    <option value="{{$vo['id']}}" @if($vo['id'] == request('usergroup_id'))selected="selected"@endif>{{$vo['name']}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i>
                            </button>
                        </div>
                    </div>
                </form>
                <script type="text/html" id="toolbar">
                    <div class="layui-btn-container">
                        @if(_can('admin.basic.membercreate'))
                            <a class="layui-btn layui-btn-sm" onclick="getIds()"><i class="layui-icon"></i>添加用户</a>
                        @endif
                        @if(_can('admin.basic.membercreate'))
                            <form class="layui-form layui-col-space5 layui-input-inline" action="{{route('admin.basic.membercreate.attach')}}">
                                @csrf
                                <input type="hidden" name="batch_id" value="{{request('batch_id')}}">
                                <input type="hidden" name="basic_id" value="{{request('basic_id')}}">
                                <a class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="layui-icon"></i>添加查询结果</a>
                            </form>
                        @endif
                            <a href="{{route('admin.basic.memberindex',['batch_id'=>request('batch_id'),'basic_id'=>request('basic_id')])}}" class="layui-btn layui-btn-sm">返回</a>
                    </div>
                </script>
                <table lay-filter="table-hide" id="demo"  {{--lay-data="{height:'full-310', cellMinWidth: 80,toolbar: '#toolbar',defaultToolbar:['filter'],limit:{{$user['per_page']}}}"--}}>

                </table>
                @include("layout.page")
            </div>

        </div>
    </div>

@endsection
@section('script')
    <script src="{{asset('layui/modules/xm-select.js')}}"></script>
    <script src="{{asset('js/tree/select.js')}}"></script>
    <script>
        var idx = -1;
        layui.use(['form','table','xmSelect'], function () {
            var table = layui.table;
            var form = layui.form,xmSelect = layui.xmSelect;
            var arr = [];
            let org = selectTreeParams('#org_id','org_id','请选择组织机构',"{{request('org_id')}}", {!! $org !!},function (data){
                $("#org_code").val(data['arr'][0]['code'])
            });
            table.render({
                elem: '#demo'
                ,height:'full-310'
                ,cellMinWidth:80
                ,data: {!! $user !!} //数据接口
                ,page: {
                    layout: ['prev', 'page', 'next','count','skip','limit' ] //自定义分页布局
                        ,prev:"上一页"
                        ,next:"下一页"
                        ,limit:15
                        ,limits:[15,30,50]
                }
                ,cols: [[ //表头
                    {field: 'id', width:80, type:'checkbox'}
                    ,{field: 'username', title: '用户名' }
                    ,{field: 'truename', title: '真实姓名' }
                    ,{field: 'post_name', title: '岗位名称'}
                    ,{field: 'org_name', title: '所属部门'}

                ]],
                defaultToolbar:['filter'],
                toolbar:"#toolbar"
            });
            xmSelect.render(org)
            window.getIds = function(){
                var checkStatus = table.checkStatus('demo')
                var arr = [];
                if(checkStatus['data'].length == 0){
                    layer.msg("请先选择人员",{icon:5});
                    return false;
                }
                $.each(checkStatus['data'],function(index,value){
                    arr.push(value['username']);
                });
                layer.confirm('你确认添加用户到考场吗？',{
                    btn: ['确认', '取消']
                }, function () {
                    $.ajax({
                        type: "POST",
                        url: '{{route("admin.basic.membercreate.attach")}}',
                        data:{
                            group:arr,
                            _token:'{{ csrf_token() }}',
                            basic_id:'{{request('basic_id')}}',
                            batch_id:'{{request('batch_id')}}'
                        },
                        success: function(res) {
                            var icon = 5;
                            if(res.code == 0){
                                icon = 6;
                            }
                            layer.msg(res.msg,{icon:icon},function(){
                                if (res.code == 0) {
                                    window.location.reload();
                                }
                            })
                        },
                    });
                }, function () {
                    layer.close();
                });
            }
            form.on("submit(*)", function (data) {
                var obj = $(this);
                obj.attr("disabled", "disabled").addClass("layui-disabled");
                var userData = {!! $user !!};
                if(userData.length == 0){
                    layer.msg("当前结果无数据",{icon:5});
                    obj.removeAttr("disabled").removeClass("layui-disabled");
                    return false;
                }
                layer.confirm('你确认添加全部查询结果考场吗？',{
                    btn: ['确认', '取消']
                }, function () {
                    $.each(userData,function(index,value){
                        arr.push(value['username'])
                    });
                    data.field.group = arr;
                    $.post(data.form.action, data.field, function (data) {
                        var icon = 5;
                        if (data.code == "0") {
                            icon = 6;
                        }
                        layer.msg(data.msg, {icon: icon, time: 1500}, function () {   //提示的插件，可以使用alert代替
                            if (data.code==0) {
                                setTimeout(function () {
                                    //刷新父页面
                                    window.location.reload();
                                }, 500);
                            } else {
                                obj.removeAttr("disabled").removeClass("layui-disabled");
                            }
                        });
                    }, 'json');
                    return false;
                }, function () {
                    layer.close();
                });
            });
        });
    </script>
@endsection
